package com.zongzi.stock;

/**
 * Created by Dell on 2017/5/10.
 */

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;

import com.facebook.common.references.CloseableReference;
import com.facebook.datasource.DataSource;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.lzy.okgo.OkGo;
import com.squareup.leakcanary.LeakCanary;
import com.zongzi.stock.utils.DensityUtils;
import java.util.concurrent.Executor;


/**
 * Created by Dell on 2017/3/15.
 */

public class MyApplication extends Application {

    private static Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();

        mContext = getApplicationContext();

        //初始化 Fresco, Resize可以设置任意图片格式
        ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)
                .setDownsampleEnabled(true)
                .build();
        Fresco.initialize(this, config);

        //必须调用初始化 OkGo
        OkGo.init(this);
        //必须调用初始化 LeakCanary
        LeakCanary.install(this);
        //在第一次调用RichText之前先调用RichText.initCacheDir()方法设置缓存目录，如果未调用则将不会进行本地缓存
    }

    public static Context getContext(){
        return mContext;
    }

    //给图片限制大小
    public static void showImage(Uri uri, SimpleDraweeView draweeView){
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(DensityUtils.dp2px(mContext,144),
                        DensityUtils.dp2px(mContext,144)))
                .build();

        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(draweeView.getController())
                .setControllerListener(new BaseControllerListener<ImageInfo>())
                .build();
        draweeView.setController(controller);
    }

    private static final Executor UiExecutor = new Executor() {
        final Handler sHandler = new Handler(Looper.getMainLooper());

        @Override
        public void execute(Runnable runnable) {
            sHandler.post(runnable);
        }
    };

    //ImageView 采用 Fresco 加载
    public static void displayImageView(final ImageView view, String url) {
        final ImageRequest request = ImageRequestBuilder
                .newBuilderWithSource(Uri.parse(url))
                .build();
        Fresco.getImagePipeline()
                .fetchDecodedImage(request, view.getContext().getApplicationContext())
                .subscribe(new BaseBitmapDataSubscriber() {
                    @Override
                    protected void onNewResultImpl(Bitmap bitmap) {
                        if (bitmap != null) {
                            view.setImageBitmap(bitmap);
                        }
                    }

                    @Override
                    protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {

                    }
                }, UiExecutor);
    }

}

